@ECHO OFF
SET SH_FILE_DIR=%0\..
SET PROJECT_DIR=%SH_FILE_DIR%\..
SET GOPATH=%PROJECT_DIR%\..\..

IF "%GOOS%"=="" (
    SET GOOS=windows
)
IF "%GOOS%"=="windows" (
    SET suffix=.exe
)


del /q "%GOPATH%\release\pac*"
del /q "%PROJECT_DIR%\proto\*.pb.go"


SET shouldGetDependentTools=F
IF NOT EXIST "%GOPATH%\bin\dep.exe" SET shouldGetDependentTools=T
IF NOT EXIST "%GOPATH%\bin\protoc-gen-go.exe" SET shouldGetDependentTools=T
IF "%shouldGetDependentTools%"=="T" (
    ECHO "get dependent tools..."
    CALL %SH_FILE_DIR%\win-go-get-tools.bat
    IF %ERRORLEVEL% NEQ 0 (
        EXIT /B 1
    )
)


IF EXIST %PROJECT_DIR%\Gopkg.toml (
    ECHO "dep ensure..."
    CALL %SH_FILE_DIR%\win-dep-ensure.bat
) ELSE (
    ECHO "dep init..."
    CALL %SH_FILE_DIR%\win-dep-init.bat
)
IF %ERRORLEVEL% NEQ 0 (
    EXIT /B 1
)


ECHO "compile protoBuf..."
protoc.exe --plugin=protoc-gen-go=%GOPATH%\bin\protoc-gen-go.exe ^
    --go_out=%PROJECT_DIR%\proto ^
    -I%PROJECT_DIR%\proto %PROJECT_DIR%\proto\pb.proto

ECHO "build pac%suffix%..."
go build -o %GOPATH%\release\pac%suffix% ^
    %PROJECT_DIR%\main\main.go
